package lion.web.interceptors.cluster;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

/**
 * 将httpsession转为经过级封装的sesion
 * 
 * <pre>
 * time   2013 2013年12月19日 下午4:47:58
 * mail   hl_0074@sina.com
 * </pre>
 * 
 * @author lion
 */
public class ClusteredSessionRequestWrapper extends HttpServletRequestWrapper {

	private ClusteredSession session;
	private String sid;

	public ClusteredSessionRequestWrapper(String id, HttpServletRequest request) {

		super(request);
		this.sid = id;
	}

	@Override
	public HttpSession getSession() {

		if (session == null) {
			session = new ClusteredSession(sid, super.getSession());
		}
		return session;
	}

	@Override
	public HttpSession getSession(boolean create) {

		if (session == null) {
			session = new ClusteredSession(sid, super.getSession(create));
		}
		return session;
	}

	public void synchroSession() {

		if (session == null) { return; }
		session.sync();
	}

}
